package com.example.userservice.novel.service;

import com.example.userservice.novel.entity.GenerationHistoryEntity;
import com.example.userservice.novel.entity.NovelGenerateRequest;
import com.example.userservice.novel.entity.NovelGenerateResponse;
import com.example.userservice.novel.entity.NovelStructure;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Date;

@Service
@Transactional
public class EnhancedNovelService {

    @Autowired
    private SimpleNovelService novelService; // 原有的生成服务

    @Autowired
    private NovelStorageService storageService;

    /**
     * 生成并保存小说架构
     */
    public NovelGenerateResponse generateAndSaveNovel(NovelGenerateRequest request) {
        long startTime = System.currentTimeMillis();
        GenerationHistoryEntity history = new GenerationHistoryEntity();
        history.setNovelName(request.getNovelName());
        history.setRequestData(request.toString());
        history.setCreatedAt(new Date());

        try {
            // 生成小说架构
            NovelStructure structure = novelService.generateNovelStructure(request.getNovelName());
//            structure.setGenre(request.getGenre());

            // 保存到数据库
            NovelStructure savedStructure = storageService.saveNovelStructure(structure);
            long generateTime = System.currentTimeMillis() - startTime;

            // 记录成功历史
            history.setSuccess(true);
            history.setResponseData(savedStructure.toString());
            history.setGenerateTime(generateTime);
            history.setNovelStructureId(savedStructure.getId());
            storageService.saveGenerationHistory(history);

//            log.info("小说生成并保存成功: {} (ID: {}), 耗时: {}ms",
//                    request.getNovelName(), savedStructure.getId(), generateTime);
            System.out.println("小说生成并保存成功");
            return NovelGenerateResponse.success(savedStructure, generateTime);

        } catch (Exception e) {
            // 记录失败历史
            long generateTime = System.currentTimeMillis() - startTime;
            history.setSuccess(false);
            history.setErrorMessage(e.getMessage());
            history.setGenerateTime(generateTime);
            storageService.saveGenerationHistory(history);

//            log.error("小说生成失败: {}", request.getNovelName(), e);
            System.out.println("小说生成失败");
            return NovelGenerateResponse.error(e.getMessage());
        }
    }

    /**
     * 分步骤生成并保存
     */
    public NovelGenerateResponse generateStepByStepAndSave(NovelGenerateRequest request) {
        long startTime = System.currentTimeMillis();
        GenerationHistoryEntity history = new GenerationHistoryEntity();
        history.setNovelName(request.getNovelName());
        history.setRequestData(request.toString());
        history.setCreatedAt(new Date());

        try {
            // 分步骤生成
            NovelStructure structure = novelService.generateStepByStep(request.getNovelName());
//            structure.setGenre(request.getGenre());

            // 保存到数据库
            NovelStructure savedStructure = storageService.saveNovelStructure(structure);
            long generateTime = System.currentTimeMillis() - startTime;

            // 记录成功历史
            history.setSuccess(true);
            history.setResponseData(savedStructure.toString());
            history.setGenerateTime(generateTime);
            history.setNovelStructureId(savedStructure.getId());
            storageService.saveGenerationHistory(history);

            return NovelGenerateResponse.success(savedStructure, generateTime);

        } catch (Exception e) {
            long generateTime = System.currentTimeMillis() - startTime;
            history.setSuccess(false);
            history.setErrorMessage(e.getMessage());
            history.setGenerateTime(generateTime);
            storageService.saveGenerationHistory(history);

//            log.error("分步骤生成失败: {}", request.getNovelName(), e);
            System.out.println("分步骤生成失败" + request.getNovelName());
            return NovelGenerateResponse.error(e.getMessage());
        }
    }
}
